<?php
#by reemo 2012
class ShowAcademyPage extends AbstractPage
{	
	public static $requireModule = 0;

	function __construct() 
	{
		parent::__construct();
	}
	
	
	private function calc($Element)
	{
		global $PLANET, $USER, $resource, $pricelist, $CONF;
		
		$elementLevel = $USER[$resource[$Element]];
		
		$cost 		= floor($pricelist[$Element]['cost']['901'] * pow($pricelist[$Element]['factor'], $elementLevel));
		
		return($cost);
	}
	
	function up()
	{
		global $PLANET, $USER, $resource, $reslist;
		
		$Element     		= HTTP::_GP('skil', 0);
		$Elements			= $reslist['skils'];
		
		$cost =  $this->calc($Element);
		
		if (!in_array($Element,$Elements) || $cost > $USER['exp_point'] || !BuildFunctions::isTechnologieAccessible($USER, $PLANET, $Element))
		{
			$this->redirectTo('game.php?page=academy');
			return;
		}
		
		$USER['exp_point'] -= $cost;

		$GLOBALS['DATABASE']->query("UPDATE ".USERS." SET
				 `exp_point` = `exp_point` -".$cost.",
				 `".$resource[$Element]."` = `".$resource[$Element]."` + 1
				 WHERE `id` = '".$USER["id"]."';");		

		$this->redirectTo('game.php?page=academy');
	}
	
		
	public function show()
	{
		global $ProdGrid, $LNG, $resource, $reslist, $CONF, $PLANET, $USER, $pricelist, $requeriments;
		
		$SkilsInfoList      = array();

		$Elements			= $reslist['skils'];
		
		foreach($Elements as $ID => $Element)
		{
			
			$sost = BuildFunctions::isTechnologieAccessible($USER, $PLANET, $Element);
			$trebov = "<br /><br />Требуется:<br />";
			if(isset($requeriments[$Element]))
				{
					foreach($requeriments[$Element] as $requireID => $RedCount)
					{
						if($RedCount > $USER[$resource[$requireID]])
						$trebov .= "<span style='color:#F00;'>".$LNG['tech'][$requireID]." (уровень ".$USER[$resource[$requireID]]."/".$RedCount.")</span><br />";
					}
				}
			$cost =  $this->calc($Element);
			
			if($trebov == "<br /><br />Требуется:<br />")
				if($cost<$USER['exp_point'])
						$trebov .="<span style='color:#0C0;'>".$cost."</span> Очков опыта";	
				else	
						$trebov .="<span style='color:#F00;'>".$cost."</span> Очков опыта";
						
			$bonus1		= sprintf($LNG['shortDescription'][$Element],$pricelist[$Element]['cost']['902']*($USER[$resource[$Element]]+1),$pricelist[$Element]['cost']['903']*($USER[$resource[$Element]]+1));

			
			$text 		= "<span style='color:#09f;font-weight:bold;'>".$LNG['tech'][$Element]."</span><br />Уровень ".($USER[$resource[$Element]]+1).":<br />".$bonus1.$trebov;
			
			$SkilsInfoList[$Element]	= array(
				'level'				=> $USER[$resource[$Element]],
				'text'				=> $text,
				'sost'				=> $sost,
				'cost'				=> $cost,
			);
		}

		
		
		$this->tplObj->assign_vars(array(
			'skils'					=> $SkilsInfoList,
			'point'			 		=> $USER['exp_point'],
		));
			
		$this->display('page.academy.tpl');
	}
}
?>